﻿extends Node

# 武器类型枚举
enum WeaponType {
	MELEE,
	RANGED
}
# 武器数据缓存
var weapon_data_cache: Dictionary =  {}
const WEAPONS_DATA_PATH           := "globals/data/weapons_data.json"


func _ready() -> void:
	load_all_weapon_data()


# 加载所有武器数据
func load_all_weapon_data() -> void:
	if FileAccess.file_exists(WEAPONS_DATA_PATH):
		var json_string: String = FileAccess.get_file_as_string(WEAPONS_DATA_PATH)
		var json                = JSON.new()
		var error               = json.parse(json_string)

		if error == OK:
			weapon_data_cache = json.data
			print("武器数据加载成功")
		else:
			printerr("解析武器数据JSON出错: ", json.get_error_message())
	else:
		printerr("找不到武器数据文件: ", WEAPONS_DATA_PATH)


# 获取指定武器的数据
func get_weapon_data(weapon_id: String) -> Dictionary:
	if weapon_data_cache.has(weapon_id):
		return weapon_data_cache[weapon_id]

	printerr("未找到武器数据: ", weapon_id)
	return {}


# 获取武器类型
func get_weapon_type(weapon_id: String) -> int:
	var data: Dictionary = get_weapon_data(weapon_id)
	if data.has("type"):
		match data["type"]:
			"melee": return WeaponType.MELEE
			"ranged": return WeaponType.RANGED
			_: return -1
	return -1


# 获取所有武器ID
func get_all_weapon_ids() -> Array:
	return weapon_data_cache.keys()